package com.fr.data.pool;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

public class ConnectionPoolManager
{
  private static ConnectionPoolManager connectionPoolManager;
  private Map m_poolTable = null;
  private List logList = new ArrayList();

  public static ConnectionPoolManager getConnectionPoolManager()
  {
    if (connectionPoolManager == null)
      connectionPoolManager = new ConnectionPoolManager();
    return connectionPoolManager;
  }

  public void addLog(String paramString)
  {
    this.logList.add(paramString);
  }

  public void removeLog(String paramString)
  {
    this.logList.remove(paramString);
  }

  public String[] getLogs()
  {
    return ((String[])this.logList.toArray(new String[this.logList.size()]));
  }

  public String[] getPoolManagerInfo()
  {
    if ((this.m_poolTable == null) || (this.m_poolTable.size() == 0))
      return new String[] { "No PoolObject" };
    String[] arrayOfString = new String[this.m_poolTable.size()];
    Iterator localIterator = this.m_poolTable.entrySet().iterator();
    for (int i = 0; localIterator.hasNext(); ++i)
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      String str = (String)localEntry.getKey();
      StringBuffer localStringBuffer = new StringBuffer();
      localStringBuffer.append(Inter.getLocText("Link") + ":").append(str.substring(0, str.indexOf("&"))).append("<br>");
      if (!(localEntry.getValue() instanceof BasicDataSource))
        return new String[] { "Error: Not hoped value." };
      BasicDataSource localBasicDataSource = (BasicDataSource)localEntry.getValue();
      localStringBuffer.append(Inter.getLocText("ConnectionPool") + Inter.getLocText("Saved_Connection_Count") + ":").append(localBasicDataSource.getNumIdle()).append("<br>");
      localStringBuffer.append(Inter.getLocText("Actived") + Inter.getLocText("Link") + ":").append(localBasicDataSource.getNumActive()).append("<br>");
      arrayOfString[i] = localStringBuffer.toString();
    }
    return arrayOfString;
  }

  public synchronized DataSource getDataSource(String paramString1, String paramString2, String paramString3, String paramString4, DBCPConnectionPoolAttr paramDBCPConnectionPoolAttr)
  {
    String str1 = paramString1 + paramString2 + "&" + paramString3 + paramString4 + ((paramDBCPConnectionPoolAttr == null) ? null : paramDBCPConnectionPoolAttr.toValueListString());
    Object localObject = (DataSource)this.m_poolTable.get(str1);
    if (localObject == null)
    {
      localObject = new BasicDataSource();
      ((BasicDataSource)localObject).setDriverClassName(paramString1);
      ((BasicDataSource)localObject).setUrl(paramString2);
      ((BasicDataSource)localObject).setUsername(paramString3);
      ((BasicDataSource)localObject).setPassword(paramString4);
      if (paramDBCPConnectionPoolAttr == null)
        paramDBCPConnectionPoolAttr = new DBCPConnectionPoolAttr();
      ((BasicDataSource)localObject).setInitialSize(paramDBCPConnectionPoolAttr.getInitialSize());
      ((BasicDataSource)localObject).setMaxActive(paramDBCPConnectionPoolAttr.getMaxActive());
      ((BasicDataSource)localObject).setMaxIdle(paramDBCPConnectionPoolAttr.getMaxIdle());
      ((BasicDataSource)localObject).setMinIdle(paramDBCPConnectionPoolAttr.getMinIdle());
      ((BasicDataSource)localObject).setMaxWait(paramDBCPConnectionPoolAttr.getMaxWait());
      ((BasicDataSource)localObject).setTestOnBorrow(paramDBCPConnectionPoolAttr.isTestOnBorrow());
      ((BasicDataSource)localObject).setTestOnReturn(paramDBCPConnectionPoolAttr.isTestOnReturn());
      ((BasicDataSource)localObject).setTestWhileIdle(paramDBCPConnectionPoolAttr.isTestWhileIdle());
      ((BasicDataSource)localObject).setMinEvictableIdleTimeMillis(paramDBCPConnectionPoolAttr.getMinEvictableIdleTimeMillis());
      ((BasicDataSource)localObject).setTimeBetweenEvictionRunsMillis(paramDBCPConnectionPoolAttr.getTimeBetweenEvictionRunsMillis());
      ((BasicDataSource)localObject).setNumTestsPerEvictionRun(paramDBCPConnectionPoolAttr.getNumTestsPerEvictionRun());
      if ((paramDBCPConnectionPoolAttr.isTestOnBorrow()) || (paramDBCPConnectionPoolAttr.isTestOnReturn()) || (paramDBCPConnectionPoolAttr.isTestWhileIdle()))
      {
        String str2 = paramDBCPConnectionPoolAttr.getValidationQuery();
        if (StringUtils.isBlank(str2))
          str2 = getDefaultValidationQuery((DataSource)localObject);
        if (StringUtils.isNotBlank(str2))
        {
          ((BasicDataSource)localObject).setValidationQuery(str2);
          FRContext.getLogger().log(Level.INFO, "ValidationQuery is " + str2);
        }
      }
      this.m_poolTable.put(str1, localObject);
    }
    return ((DataSource)localObject);
  }

  private String getDefaultValidationQuery(DataSource paramDataSource)
  {
    Connection localConnection = null;
    try
    {
      String str1;
      localConnection = paramDataSource.getConnection();
      DatabaseMetaData localDatabaseMetaData = localConnection.getMetaData();
      ResultSet localResultSet1 = localDatabaseMetaData.getTableTypes();
      do
        if (!(localResultSet1.next()))
          break label134;
      while (!("TABLE".equals(localResultSet1.getString("TABLE_TYPE"))));
      ResultSet localResultSet2 = localDatabaseMetaData.getTables(null, null, "%", new String[] { "TABLE" });
      do
      {
        if (!(localResultSet2.next()))
          break label134;
        str1 = localResultSet2.getString("TABLE_NAME");
      }
      while (!(StringUtils.isNotBlank(str1)));
      String str2 = "select count(*) from " + str1 + " where 1 = 2;";
      jsr 47;
      label134: return str2;
    }
    catch (SQLException localSQLException1)
    {
      try
      {
        if (localConnection != null)
        {
          localConnection.close();
          localConnection = null;
        }
      }
      catch (SQLException localSQLException2)
      {
      }
      Object localObject1 = null;
      return localObject1;
    }
    finally
    {
      try
      {
        if (localConnection != null)
        {
          localConnection.close();
          localConnection = null;
        }
      }
      catch (SQLException localSQLException3)
      {
      }
    }
    return null;
  }
}